<?php
require_once '../entity/Notification.php';
require_once '../entity/NotificationType.php';
require_once '../entity/Masker.php';
require_once 'DaoUtils.php';
require_once 'MaskerDao.php';

class NotificationDao extends DaoUtils {
	public function getCountNotificationToView($idMasker) {
		$query = " SELECT COUNT(*) AS COUNT ";
		$query .= "FROM " . Notification::getTableName() . " WHERE id_masker_to = " . $idMasker . " and view = 'N'";

		$record = $this -> selectRecord($query);
		return $record;
	}
	
	public function updateNotificationView($idMasker) {
		$query = "UPDATE " . Notification::getTableName() . " SET view = 'Y' WHERE id_masker_to = " . $idMasker . " and view = 'N'";

		$result = $this -> update($query);
		return $result;
	}
	
	public function getNotifications($idMasker) {
		$language = $this -> getLanguage();
		$query = " SELECT n.id,n.id_masker_to,n.id_masker_from,m.name as name_masker_from,n.text,n.link,n.notification_type,n.view ";
		$query .= "FROM " . Notification::getTableName() . " n INNER JOIN " . Masker::getTableName() . " m on n.id_masker_from = m.id WHERE id_masker_to = " . $idMasker. " ORDER BY n.id DESC  LIMIT 30";

		$records = $this -> selectRecords($query);
		$notificationArray = array();
		
		foreach ($records as $record)  {
			$notification = new Notification();
			$notification -> setNotification($record['id'],$record['id_masker_to'], $record['id_masker_from'], $record['name_masker_from'],
			 $record['text'], $record['link'], $record['notification_type'], $record['view']);
			$notification->text = $this->getTranslatedText($language, $notification->text);
			array_push($notificationArray,$notification);
		} 
		return $notificationArray;
	}

	function getTranslatedText($language, $string) {
		$pattern = "/<span class='notificationLabel'?.*>(.*)<\/span>/";
	    preg_match($pattern, $string, $matches);
	    if (sizeof($matches) > 0) {
	   		$translatedText =  $language->getLabel($matches[1]);
			return preg_replace($pattern, $translatedText, $string);
	    }
	}

	public function insertNotification($idMaskerFrom, $idMaskerTo, $notificationType) {
		$this ->  insertNotificationWithText($idMaskerFrom, $idMaskerTo, $notificationType, null);
	}
	public function insertNotificationWithText($idMaskerFrom, $idMaskerTo, $notificationType, $text_to_text) {
		// if notification is for the anonymous, don't insert it
		if ($idMaskerTo == 0 || $idMaskerTo == $idMaskerFrom) {
			return false;
		}
		$maskerDao = new MaskerDao();
	
		$masker = $maskerDao->getMasker($idMaskerFrom);
		$maskerFromName = $masker["name"];
		
		$masker = $maskerDao->getMasker($idMaskerTo);
		$maskerToName = $masker["name"];
		
		switch ($notificationType) {
			case (NotificationType::$follow):
				$text = "<span class=''notificationFromName''>".$maskerFromName."</span><span class=''notificationLabel''>notification.follow.text</span>";
				$link ="home.php#profile?masker=".$maskerFromName;
				break;
			case (NotificationType::$unfollow):
				$text = "<span class=''notificationFromName''>".$maskerFromName."</span><span class=''notificationLabel''>notification.unfollow.text</span>";
				$link ="home.php#profile?masker=".$maskerFromName;
				break;
			case (NotificationType::$signColorfeel):
				$text = "<span class=''notificationFromName''>".$maskerFromName."</span><span class=''notificationLabel''>notification.colorfeel.text</span>";
				$link ="home.php#profile_signs?masker=".$idMaskerTo."&maskerName=".$maskerToName;
				break;
			case (NotificationType::$signReply):
				$text = "<span class=''notificationFromName''>".$maskerFromName."</span><span class=''notificationLabel''>notification.reply.text</span>";
				$link ="home.php#profile_signs?masker=".$idMaskerFrom."&maskerName=".$maskerFromName;
				if ($text_to_text != null) {
					$text .= " \"". $this->cutText($text_to_text) ."\"";
				}
				break;
			case (NotificationType::$question):
				$text = "<span class=''notificationFromName''>".$maskerFromName."</span><span class=''notificationLabel''>notification.question.text</span>";
				$link ="home.php#questions?masker=".$maskerToName;
				if ($text_to_text != null) {
					$text .= " \"". $this->cutText($text_to_text) ."\"";
				}
				break;
			case (NotificationType::$questionReply):
				$text = "<span class=''notificationFromName''>".$maskerFromName."</span><span class=''notificationLabel''>notification.question.reply.text</span>";
				$link ="home.php#questions?masker=".$maskerToName;
				if ($text_to_text != null) {
					$text .= " \"". $this->cutText($text_to_text) ."\"";
				}
				break;
			default:
				echo "no notification type found";
				return false;
		}
		
		$query = "insert into " . Notification::getTableName() . " (id_masker_to, id_masker_from, text, link, notification_type, view) ";
		$query .= " values (" . $idMaskerTo . "," . $idMaskerFrom . ",'" . $text . "','" . $link . "','" . $notificationType . "','N') ";
		
		$result = $this -> insert($query);

		return $result;
	}
	
	private function getLanguage() {
		$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
		if ($lang == "it") {
			include '../resources/Message_IT.php';
		} else if ($lang == "en") {
			include '../resources/Message_EN.php';
		} else {
			include '../resources/Message_EN.php';
		}
		return new Language();
	}
	
	private function cutText($text) {
		return (strlen($text) > 50) ? substr($text,0,50).'...' : $text;
	}
}
?>